import 'package:ble_measure_plugin/devices/base_device_model.dart';
import 'package:ble_measure_plugin/model/yuwell_pressure_model.dart';
import 'package:ble_measure_plugin/utils/ext.dart';
import 'package:flutter_blue/flutter_blue.dart';

import '../ble_measure_plugin.dart';
import '../utils/math_utils.dart';

class AiaolePressureDeviceModel extends BaseDeviceMeasureModel {
  var pressureServiceUuid = "00001000-0000-1000-8000-00805f9b34fb";
  var CHARACTERISTIC_WRITEABLE = "00001001-0000-1000-8000-00805f9b34fb";
  var CHARACTERISTIC_READABLE = "00001002-0000-1000-8000-00805f9b34fb";

  EventHandlerMap<String>? _pressureMeasuringHandler;
  EventHandlerMap<YuwellPressureModel>? _pressureResultHandler;
  EventHandlerMap<String>? _errorHandler;

  void addListener({
    EventHandlerMap<YuwellPressureModel>? resultListener,
    EventHandlerMap<String>? measureListener,
    EventHandlerMap<String>? error,
  }) {
    _pressureResultHandler = resultListener;
    _errorHandler = error;
    _pressureMeasuringHandler = measureListener;
  }

  @override
  void onReceiveData(List<String> data) {
    _setPressureData(data);
  }

  void _setPressureData(List<String> data) {
    var dataType = data[2];
    switch (dataType) {
      case "02":

        if(hasMeasure){
          var measureValue =
          int.parse(data.sublist(5, 6).compare16ToStr(), radix: 16);
          if (_pressureMeasuringHandler != null) {
            _pressureMeasuringHandler!(measureValue.toString());
          }
        }
        hasMeasure = true;
        break;

      case "03":
        var model = YuwellPressureModel();
        model.measureStatus = "1";
        var  retValueLast = 0;
        // var measureValue =
        //     int.parse(data.sublist(9, 10).compare16ToStr(), radix: 16);
        String s = MathUtil.HToB(MathUtil.byteToHexString(int.parse(data[10])));

        if (s.length == 8) {
          if ("1"==(s.substring(7,8))) {
            retValueLast  = 256;
          }
        }
        var retValue =  (int.parse(data[9],radix: 16) & 0x0FF) + retValueLast;
        // print("value:${int.parse(data[9],radix: 16) & 0x0FF}\nretValueLast:${retValueLast}\nretValue:${retValue}");
        model.highPressure = retValue.toString();
        var lowValue = int.parse(data[11], radix: 16);
        model.lowPressure = lowValue.toString();
        var heartValue = int.parse(data[12], radix: 16).toString();
        model.pulse = heartValue;
        if (_pressureResultHandler != null && hasMeasure) {
          _pressureResultHandler!(model);
        }
        hasMeasure = false;
        break;
      case "EE":
        if (_errorHandler != null) {
          _errorHandler!("测量错误${data[3]}");
        }
        break;
      case "05":
        print("测量结束");
        break;
    }
  }

  @override
  String get serviceUuid => pressureServiceUuid;

  @override
  String get characteristicReadable => CHARACTERISTIC_READABLE;

  @override
  String get characteristicWriteable => CHARACTERISTIC_WRITEABLE;

  @override
  void release() {
    _pressureMeasuringHandler = null;
    _pressureResultHandler = null;
    _errorHandler = null;
    super.release();
  }
}
